package sskj.regulatory.presentation.screen.pmdust

import android.app.AlertDialog
import android.net.http.SslError
import android.view.KeyEvent
import android.webkit.*
import com.vondear.rxtools.RxWebViewTool.initWebView
import kotlinx.android.synthetic.main.fragment_webview.view.*
import sskj.regulatory.R
import sskj.regulatory.presentation.base_mvp.base.BaseFragment
import javax.inject.Inject

class PmDustFragment : BaseFragment<PmDustContract.View, PmDustContract.Presenter>(), PmDustContract.View {

    @Inject
    lateinit var mPresenter: PmDustPresenter

    override fun injectDependencies() = activityComponent.inject(this)

    override fun onBackPressed(): Boolean {
        return when (mCacheView.mwebview.canGoBack()) {
            true -> {
                mCacheView.mwebview.goBack()
                false
            }
            else -> {
                true
            }
        }

    }

    override fun translucentFull() = false

    override fun initPresenter() = mPresenter

    override val layoutResId: Int = R.layout.fragment_webview

    override fun initView() {
        initTopBar()
        initWebView()
    }

    private fun initWebView() {

        mCacheView.mwebview.webViewClient = object : WebViewClient() {

            override fun onReceivedSslError(view: WebView, handler: SslErrorHandler, error: SslError) {
                handler.proceed()
            }
        }

        mCacheView.mwebview.loadUrl("http://1853j02h31.iok.la/appToEquipStatus.do?userName=administrator&password=kingfree")
    }


    private fun initTopBar() {
        mCacheView.topBarWebview.setTitle("扬尘监控")
        mCacheView.topBarWebview.addLeftBackImageButton().setOnClickListener {
            when (mCacheView.mwebview.canGoBack()) {
                true -> mCacheView.mwebview.goBack()
                else -> popBackStack()
            }
        }
    }
}